package com.iot08.admin.controller;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClients;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;

/**
 * @version 1.0
 * @Description: 勿删
 * @time: 2021/3/15 15:50
 * @author: Rock
 */
@Slf4j
public class TestMongo {


    public static void main(String[] args) {
        String db = "sys_user";
        String oldUri = "mongodb://47.106.71.153:27017";
        String oldDatabase = "admin-test";
        MongoTemplate oldMongoTemplate = new MongoTemplate(new SimpleMongoClientDatabaseFactory(MongoClients.create(oldUri), oldDatabase));
        MappingMongoConverter oldMongoMapping = (MappingMongoConverter) oldMongoTemplate.getConverter();
        oldMongoMapping.afterPropertiesSet();
        oldMongoMapping.setTypeMapper(new DefaultMongoTypeMapper(null));

        String nowUri = "mongodb://120.25.241.128:27017";
        String nowDatabase = "admin-test";
        MongoTemplate nowMongoTemplate = new MongoTemplate(new SimpleMongoClientDatabaseFactory(MongoClients.create(nowUri), nowDatabase));
        MappingMongoConverter nowMongoMapping = (MappingMongoConverter) nowMongoTemplate.getConverter();
        nowMongoMapping.afterPropertiesSet();
        nowMongoMapping.setTypeMapper(new DefaultMongoTypeMapper(null));


        Document query = new Document();
        query.put("deleted", 0);
        FindIterable<Document> document = oldMongoTemplate.getCollection(db).find(query);
        if (document != null) {
            for (Document doc : document) {
                log.info("doc={}", doc);
                nowMongoTemplate.getCollection(db).insertOne(doc);
            }
        }

    }
}
